package ysq.xyg.sale.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.sale.dao.SaleDetailDao;
import ysq.xyg.sale.model.po.SaleDetail;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sale.model.vo.SaleDetailVo;
import ysq.xyg.sale.service.SaleDetailService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


/**
 * 销售模块-订单信息service实现
 *
 * @author shengQiang yu
 * @date 2019-9-9 14:28:03
 */
@Service("saleDetailService")
public class SaleDetailServiceImpl extends BaseServiceImpl<SaleDetail> implements SaleDetailService {

    @Resource
    private SaleDetailDao saleDetailDao;

    private SaleDetail saleDetail;

    @Autowired
    public void setBaseDao(SaleDetailDao saleDetailDao) {
        this.baseDao = saleDetailDao;
    }


    /**
     * 分页查询
     *
     * @param saleDetailVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    public ReturnEntity page(SaleDetailVo saleDetailVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleDetail> list = saleDetailDao.listLeft(saleDetailVo);
        PageInfo<SaleDetail> page=new PageInfo<SaleDetail>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param saleDetailVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    public ReturnEntity addOrUpdate(SaleDetailVo saleDetailVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(saleDetailVo.getId())){
            saleDetail=new SaleDetail(currentUser);
            isUpate=false;
        }else{
            saleDetail=selectByPrimaryKey(saleDetailVo.getId());
            if(ObjectUtil.isNull(saleDetail)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            saleDetail.setUpdateTime(new Date());
            saleDetail.setUpdateUser(currentUser.getId());
        }
        if(StrUtil.isNotEmpty(saleDetailVo.getEndTimeStr())){
            saleDetail.setEndTime(DateUtil.COMMON_FULL.getTextDate(saleDetailVo.getEndTimeStr()));
        }
        saleDetail.setUserId(saleDetailVo.getUserId());
        saleDetail.setProductId(saleDetailVo.getProductId());
        saleDetail.setMoney(saleDetailVo.getMoney());
        saleDetail.setRemarks(saleDetailVo.getRemarks());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(saleDetail);
        }else{
            num=insert(saleDetail);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SaleDetail saleDetail=selectByPrimaryKey(id);
        if(saleDetail!=null){
            saleDetail.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            saleDetail.setUpdateTime(new Date());
            saleDetail.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(saleDetail);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param saleDetailVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    public List<SaleDetail> acceptList(SaleDetailVo saleDetailVo){
        Example example = new Example(SaleDetail.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(saleDetailVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",saleDetailVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return saleDetailDao.selectByExample(example);
    }
}
